package cn.yfj.org.controller;

import cn.yfj.basic.util.AjaxResult;
import cn.yfj.basic.util.PageList;
import cn.yfj.org.domain.Type;
import cn.yfj.org.query.TypeQuery;
import cn.yfj.org.service.IDetailServiceI;
import cn.yfj.org.service.ITypeServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@responsebody
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private ITypeServiceI TypeService;
    @Autowired
    private IDetailServiceI detailService;

    //增加和修改
    @PutMapping
    public AjaxResult remove(@RequestBody Type type){
        try {
            //如果id为空则添加
            if(type.getId()==null){
                TypeService.save(type);

            }else {
                //否则就调用修改方法
                TypeService.update(type);
            }
            //返回操作正确信息
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            //返回错误信息
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        try {
            TypeService.remove(id);
            detailService.conndel(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    //批量删除
    @PatchMapping
    public AjaxResult remove(@RequestBody List<Long> ids){
        try {
            TypeService.bach(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    //查询一个
    @GetMapping("/{id}")
    public Type queryById(@PathVariable Long id){
        return TypeService.queryById(id);
    }

    //查询所有
    @GetMapping
    public List<Type> queryAll(){
        return TypeService.queryAll();
    }

    //分页和高级查询
    @PostMapping
    public PageList<Type> queryPage(@RequestBody TypeQuery TypeQuery){
        return TypeService.queryPage(TypeQuery);
    }




}